package scau.erps.service.impl;import java.util.List;




import org.nutz.dao.Cnd;
import org.nutz.dao.util.cri.SqlExpressionGroup;
import org.nutz.service.IdEntityService;

import scau.erps.entity.Factory;
import scau.erps.entity.Line;
import scau.erps.entity.Sand;
import scau.erps.service.FactoryService;

/** 
 * 原材料服务实现类
 * @author outsider
 * 
 *   
 */
public class FactoryServiceImpl extends IdEntityService<Factory> implements FactoryService{

	/** 
	 * 返回所有工厂数据（FYJ记得屏蔽rent_status字段）
	 *  
	*/
	public List<Factory> getAllFactoy() {
		return this.dao().query(Factory.class, null);
	}

	/** 
	 *  返回某小组的所有工厂数据  
	 */
	public List<Factory> getFactoyByTeamId(int team_Id) {
		SqlExpressionGroup e1 = null;
		e1=Cnd.exps("team_Id", "=", team_Id);
		return this.query(Cnd.where(e1), null);
	}

	/** 
	 *  返回某小组某状态下的所有工厂数据  
	 */
	public List<Factory> getTeamFactoyByStatus(int teamId, String status) {
		SqlExpressionGroup e1 = null;
		SqlExpressionGroup e2 = null;
		e1=Cnd.exps("team_Id", "=", teamId);
		e2=Cnd.exps("status",  "=", status);
		return this.query(Cnd.where(e1).and(e2), null);
	}
	/** 
	 *  返回某小组某类个工厂数据  
	 */
	public Factory getTeamFactoyByType(int teamId, String type) {  //TODO 加入或单独筛选type字段时返回结果为空 疑似BUG
		SqlExpressionGroup e1 = null;
		SqlExpressionGroup e2 = null;
		e1=Cnd.exps("team_Id", "=", teamId);
		e2=Cnd.exps("type",  "=", type);		
        List<Factory> list=this.query(Cnd.where(e1),null);
        for (Factory factory:list){
        	if (factory.getType().equals(type)) return factory;
        }
        return null;
		
	}
	/** 
	 *  购买工厂操作
	 */
	public boolean buyFactoy(int teamId, String type) {
        Factory factory=getTeamFactoyByType(teamId, type);
		Sand sand = this.dao().fetch(Sand.class, factory.getTeam_Id());
		int cash =sand.getCash();
		cash=cash-price(type);
		if (cash>=0){                       //当前现金是否足够购买厂房
			factory.setStatus("已购买");
			this.dao().update(factory);
			sand.setCash(cash);
			this.dao().update(sand);
			return true;
		}
		else return false;
	}
	/** 
	 *  租用厂房操作
	 */
	public boolean rentFactoy(int teamId, String type) {     //TODO 1.停止租用操作？
		Factory factory=getTeamFactoyByType(teamId, type);
		factory.setRent_Status("1");
		factory.setStatus("已租用");
		this.dao().update(factory);
		return true;
	}
	/** 
	 *  出售厂房操作
	 */
	@Override
	public boolean sellFactoy(int teamId, String type) {         
		Factory factory=getTeamFactoyByType(teamId, type);
		Sand sand = this.dao().fetch(Sand.class, factory.getTeam_Id());
		List<Line> lineList=this.dao().query(Line.class, Cnd.where("team_Id", "=", teamId));
		for (Line line:lineList){
			if (type.equals("大厂房") && line.getLineNo()<=4){
				if ((line.getStatus().equals("空闲")||line.getStatus().equals("空置"))==false)
					return false;
			}
			if (type.equals("中厂房") && line.getLineNo()<=7 && line.getLineNo()>=5){
				if ((line.getStatus().equals("空闲")||line.getStatus().equals("空置"))==false)
					return false;
			}
			if (type.equals("小厂房") && line.getLineNo()==8){
				if ((line.getStatus().equals("空闲")||line.getStatus().equals("空置"))==false)
					return false;
			}
		}
		factory.setStatus("未购买");
		int cash =sand.getCash();
		int other=sand.getOther();
		
		cash=cash+price(type)-2;

		other=other+2;
		sand.setCash(cash);
		sand.setOther(other);

		this.dao().update(sand);
		this.dao().update(factory);
		return true;
	}



	/** 
	 *  支付本年度厂房租金
	 */
	public int payRentFactoy(int teamId) {
		SqlExpressionGroup e1 = null;
		SqlExpressionGroup e2 = null;
		e1=Cnd.exps("team_Id", "=", teamId);
		e2=Cnd.exps("rent_status",  "=", 1);
		List<Factory> list=this.query(Cnd.where(e1).and(e2),null); //获得本年度该小组应付租金厂房
		if (list.isEmpty()==false){
			int pay=0;
			for (Factory factory : list)    
				pay=pay+rent(factory.getType());
			Sand sand = this.dao().fetch(Sand.class, teamId);
			int cash =sand.getCash();
			cash=cash-pay;
			if (cash>=0){                    //当前现金足够支付本年度租金 更新盘面现金与租金数据
				sand.setCash(cash);
				int rent=sand.getRent();
				rent=rent+pay;
				sand.setRent(rent);
				this.dao().update(sand);
				for (Factory factory : list)   {  //若厂商下年不再需要租用厂房（已购买，已出售） 则重置租用状态
				if (factory.getStatus().equals("已租用")==false) 
				  { factory.setRent_Status("0"); 
			        this.dao().update(factory);
				  }
				}
				return pay;
			}
			return -1;  //返回支付失败flag
		}
		return 0;
	}
	private int price(String type){
		if (type.equals("大厂房")) return 40;
		if (type.equals("中厂房")) return 30;
		if (type.equals("小厂房")) return 15;
		return 0;
	}
	private int rent(String type){
		if (type.equals("大厂房")) return 6;
		if (type.equals("中厂房")) return 4;
		if (type.equals("小厂房")) return 2;
		return 0;
	}
}
